package com.inossem.allotin.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.allotin.bean.TrayReceiptTypeBean;
import com.inossem.allotin.utils.NoDoubleClickListener;

import java.util.List;

public class ReceiptTypeAdapter extends RecyclerView.Adapter<ReceiptTypeAdapter.ViewHolder> {

    private Context context;
    private List<TrayReceiptTypeBean> list;
    OnChooseReceiptType listener;

    public ReceiptTypeAdapter(Context context, List<TrayReceiptTypeBean> strings, OnChooseReceiptType listener) {
        this.context = context;
        this.listener = listener;
        this.list = strings;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_dialog, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (list.get(i).isCheck()) {
            viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.is_checked));
        } else {
            if (i % 2 == 0) {
                viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_white));
            } else {
                viewHolder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_bg));
            }
        }

        viewHolder.textView.setText(list.get(i).getReceiptName());

        viewHolder.layout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                for (TrayReceiptTypeBean trayReceiptTypeBean : list) {
                    trayReceiptTypeBean.setCheck(false);
                }
                list.get(i).setCheck(true);

                listener.receiptTypeChoose(list.get(i).getReceiptCode(), list.get(i).getReceiptName());

                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        RelativeLayout layout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            layout = itemView.findViewById(R.id.layout);
        }
    }

    public interface OnChooseReceiptType {
        void receiptTypeChoose(int receiptCode, String receiptName);
    }
}
